﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
namespace 历史曲线
{
    class net
    {
        public static string HttpDownloadFile(string sn,string start,string end)
        {
            string path=sn+"["+start + "_" + end + "].csv";
            if (File.Exists(path)) return path;
            string url = "http://quotes.money.163.com/service/chddata.html?code="+ sn + "&start="+ start + "&end="+ end + "&fields=TOPEN;TCLOSE;PCHG;HIGH;LOW;LCLOSE;CHG;VOTURNOVER";
            Console.WriteLine(url);
            HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;

            //发送请求并获取相应回应数据
            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            //直到request.GetResponse()程序才开始向目标网页发送Post请求
            Stream responseStream = response.GetResponseStream();

            //创建本地文件写入流
            Stream stream = new FileStream(path, FileMode.Create);

            byte[] bArr = new byte[9024];
            int size = responseStream.Read(bArr, 0, (int)bArr.Length);
            while (size > 0)
            {
                stream.Write(bArr, 0, size);
                size = responseStream.Read(bArr, 0, (int)bArr.Length);
                Console.Write('.');
            }
            stream.Close();
            responseStream.Close();
            return path;
        }
        
    }
}
